using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace StikiDictionary
{
    //this class adds the resource files for the new project to the language
    class ResourceFileLinker
    {
        private const string cProjectFileExtension = "csproj";
        private const string cRoot = "C:\\";
        private const string cItemGroupTag = "ItemGroup";
        private const string cEmbeddedResourceTag = "EmbeddedResource";

        public string GetFileExtension(string filename)
        {
            string[] fileparts = filename.Split('.');
            if (fileparts.Length > 0)
                return fileparts[fileparts.Length - 1];
            else
                return "";
        }

        public string GetFileName(string pathPlusFileName)
        {
            string[] fileparts = pathPlusFileName.Split('\\');
            return fileparts[fileparts.Length - 1];
        }

        private void GetProjectFile(ref string projectFilePath, ref string projectFileName)
        {
            string[] fileEntries = Directory.GetFiles(projectFilePath);
            foreach (string fileName in fileEntries)
            {
                if ((GetFileExtension(fileName) == cProjectFileExtension))
                {
                    projectFileName = GetFileName(fileName);
                    break;
                }
            }
            if (projectFileName == "")
            {
                projectFilePath = Directory.GetParent(projectFilePath).FullName;
                GetProjectFile(ref projectFilePath, ref projectFileName);
            }
        }

        private string GetDepeningOnResourceFileName(string resourceFileName)
        {
            string[] fileNameParts = resourceFileName.Split('.');

            if (fileNameParts.Length == 2)
                return resourceFileName;
            else if (fileNameParts.Length == 3)
                return fileNameParts[0] + "." + fileNameParts[2];
            else            
                return "";         
        }

        public string GetRelativePathForResourceFile(string projectFilePath, string resourceFilePath)
        {
            if (projectFilePath == resourceFilePath)
                return "";
            else if (resourceFilePath.Contains(projectFilePath))
                return resourceFilePath.Remove(0, projectFilePath.Length + 1) + "\\";
            else
                return "";
        }


        public bool AddFileToProject(string resourceFilePath, string resourceFileName) 
        {            
            string projectFilePath = resourceFilePath;
            string projectFileName = "";
            GetProjectFile(ref projectFilePath, ref projectFileName);
            string resourceFileRelativePath = GetRelativePathForResourceFile(projectFilePath, resourceFilePath) + resourceFileName;
            string depeningOnResourceFileRelativePath = GetRelativePathForResourceFile(projectFilePath, resourceFilePath) + GetDepeningOnResourceFileName(resourceFileName);

            XmlDocument projectFile = new XmlDocument();
            bool fileAdded = false;
            
            if (projectFileName != "")
            {
                projectFile.Load(projectFilePath + "\\" + projectFileName);
                XmlNode root = projectFile.DocumentElement;
                for (int i = 0; i < root.ChildNodes.Count; i++)
                    if (root.ChildNodes[i].Name == cItemGroupTag)                        
                        fileAdded =  fileAdded  || ProcessData(root.ChildNodes[i], depeningOnResourceFileRelativePath, resourceFileRelativePath);  
            }
            if (fileAdded)
                projectFile.Save(projectFilePath + "\\" + projectFileName);
            
            return fileAdded;
        }

        public bool ProcessData(XmlNode data, string resourceFileName, string newResourceFileName)
        {
            for (int i = 0; i < data.ChildNodes.Count; i++)
            {
                if (data.ChildNodes[i].Name == cEmbeddedResourceTag)
                {
                    if (data.ChildNodes[i].Attributes[0].Value == resourceFileName)
                    {
                        XmlNode newResourceNode = data.ChildNodes[i].Clone();
                        newResourceNode.Attributes[0].Value = newResourceFileName;
                        data.AppendChild(newResourceNode);
                        return true;
                    }
                }
            }
            return false;
        }             

    }
}

        
